/*****************************************************************************
 * Copyright (c) 2019, Nations Technologies Inc.
 *
 * All rights reserved.
 * ****************************************************************************
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * - Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the disclaimer below.
 *
 * Nations' name may not be used to endorse or promote products derived from
 * this software without specific prior written permission.
 *
 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
 * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * ****************************************************************************/

/**
*\*\file n32g430_it.c
*\*\author Nations
*\*\version v1.0.0
*\*\copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
**/


#include "n32g430.h"
#include "n32g430_it.h"
#include "bsp_adc.h"
#include "bsp_gpio.h"
#include "bsp_pwm.h"
#include "loop.h"
#include "app_analog.h"
#include "log.h"

/** Cortex-M4 Processor Exceptions Handlers **/

/**
*\*\name    NMI_Handler.
*\*\fun     This function handles NMI exception.
*\*\param   none
*\*\return  none 
**/
void NMI_Handler(void)
{
    bsp_pwm_output_disable();
    loop_disable(); // 环路禁止
    bsp_pwm_duty_set(0);
}

/**
*\*\name    HardFault_Handler.
*\*\fun     This function handles Hard Fault exception.
*\*\param   none
*\*\return  none 
**/
void HardFault_Handler(void)
{
    bsp_pwm_output_disable();
    loop_disable(); // 环路禁止
    bsp_pwm_duty_set(0);
    log_debug("FAULT----%s:%d\n", __FILE__, __LINE__);
    /* Go to infinite loop when Hard Fault exception occurs */
    while (1)
    {
    }
}

/**
*\*\name    MemManage_Handler.
*\*\fun     This function handles Memory Manage exception.
*\*\param   none
*\*\return  none 
**/
void MemManage_Handler(void)
{
    bsp_pwm_output_disable();
    loop_disable(); // 环路禁止
    bsp_pwm_duty_set(0);
    /* Go to infinite loop when Memory Manage exception occurs */
    while (1)
    {
    }
}

/**
*\*\name    BusFault_Handler.
*\*\fun     This function handles Bus Fault exception.
*\*\param   none
*\*\return  none 
**/
void BusFault_Handler(void)
{
    bsp_pwm_output_disable();
    loop_disable(); // 环路禁止
    bsp_pwm_duty_set(0);
    /* Go to infinite loop when Bus Fault exception occurs */
    while (1)
    {
    }
}

/**
*\*\name    UsageFault_Handler.
*\*\fun     This function handles Usage Fault exception.
*\*\param   none
*\*\return  none 
**/
void UsageFault_Handler(void)
{
    bsp_pwm_output_disable();
    loop_disable(); // 环路禁止
    bsp_pwm_duty_set(0);
    /* Go to infinite loop when Usage Fault exception occurs */
    while (1)
    {
    }
}

/**
*\*\name    SVC_Handler.
*\*\fun     This function handles SVCall exception.
*\*\param   none
*\*\return  none 
**/
void SVC_Handler(void)
{
}

/**
*\*\name    DebugMon_Handler.
*\*\fun     This function handles Debug Monitor exception.
*\*\param   none
*\*\return  none 
**/
void DebugMon_Handler(void)
{
}

/**
*\*\name    SysTick_Handler.
*\*\fun     This function handles SysTick Handler.
*\*\param   none
*\*\return  none 
**/
void SysTick_Handler(void)
{
}

/** N32G430 Peripherals Interrupt Handlers, interrupt handler's name please refer to the startup file (startup_n32g430.s) **/
void ADC_IRQHandler(void)
{
    static uint8_t cnt = 0;
    float pid_out;
    uint16_t duty;

    // 计算真实信号幅度
    app_analog_conv(ANALOG_ID_VOUT);
    app_analog_conv(ANALOG_ID_IOUT);
    app_analog_conv(ANALOG_ID_VIN);
    app_analog_conv(ANALOG_ID_IIN);

    if (++cnt >= 2 && loop_is_enable()) { // 环路计算频率为开关频率的2分频
        cnt = 0;
        // uint16_t trigger;
        // bsp_gpio_set(PORT_WIFI_EN, PIN_WIFI_EN);

        // 环路计算 结果为百分比
        pid_out = loop_cal(asignal_get_real(ANALOG_ID_VOUT), 
                           asignal_get_real(ANALOG_ID_IOUT), 
                           asignal_get_real(ANALOG_ID_VIN));

        // 占空比转换
        duty = pid_out * BSP_TIMER_CNT_RELOAD;

        bsp_pwm_duty_set(duty); // 设置占空比

        // 占空比过小时，无法在上管关断前采样完毕。
        // 为避免在上管关断时刻采样，在占空比小于阈值时，触发时刻挪到上管关闭期间的中间位置
        // if (duty < BSP_PWM_TRIG_TH) {
        //     trigger = ((BSP_PWM_PERIOD + duty) >> 1); // duty + (BSP_PWM_PERIOD - duty) / 2;
        // } else {
        //      trigger = (duty >> 1);
        // }
        // bsp_pwm_trigger_set(trigger - BSP_PWM_TRIG_NOFST); // 设置ADC触发点
        // trigger = 10;
        // bsp_pwm_trigger_set(trigger); // 设置ADC触发点

        // bsp_gpio_reset(PORT_WIFI_EN, PIN_WIFI_EN);
    }

    ADC_INTFlag_Status_Clear(ADC_INT_FLAG_JENDC);
}

